using System;
using Microsoft.Practices.Unity;
using Unity.Extensions;
using Xunit;

namespace Unity.Extensions
{
    public class InterceptInterfaceInterceptionExtensionTest
    {
        [Fact]
        public void ExceptionsAreUnchanged()
        {
            Recorder.Records.Clear();
            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<InterceptionExtension>();
            container.RegisterType<ISpy, SpyInterface>();
            container.Configure<IInterceptionConfiguration>().InterceptInterface<SpyInterface>(typeof(ISpy).GetMethod("ThrowsException"),
                                                                                               new RecordingHandler());

            ISpy obj = container.Resolve<ISpy>();

            Assert.Throws<Exception>(obj.ThrowsException);

            Assert.Equal(3, Recorder.Records.Count);
            Assert.Equal("Before Method", Recorder.Records[0]);
            Assert.Equal("In Method", Recorder.Records[1]);
            Assert.Equal("After Method", Recorder.Records[2]);
        }

        [Fact]
        public void InterceptViaAttributes()
        {
            Recorder.Records.Clear();
            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<InterceptionExtension>();
            container.RegisterType<ISpy, SpyInterfaceAttributes>();

            ISpy obj = container.Resolve<ISpy>();
            obj.InterceptedMethod();

            Assert.Equal(3, Recorder.Records.Count);
            Assert.Equal("Before Method", Recorder.Records[0]);
            Assert.Equal("In Method", Recorder.Records[1]);
            Assert.Equal("After Method", Recorder.Records[2]);
        }

        [Fact]
        public void InterceptViaCode()
        {
            Recorder.Records.Clear();
            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<InterceptionExtension>();
            container.RegisterType<ISpy, SpyInterface>();
            container.Configure<IInterceptionConfiguration>().InterceptInterface<SpyInterface>(typeof(ISpy).GetMethod("InterceptedMethod"),
                                                                                               new RecordingHandler());

            ISpy obj = container.Resolve<ISpy>();
            obj.InterceptedMethod();

            Assert.Equal(3, Recorder.Records.Count);
            Assert.Equal("Before Method", Recorder.Records[0]);
            Assert.Equal("In Method", Recorder.Records[1]);
            Assert.Equal("After Method", Recorder.Records[2]);
        }

        public interface ISpy
        {
            void InterceptedMethod();
            void ThrowsException();
        }

        internal sealed class SpyInterface : ISpy
        {
            public void InterceptedMethod()
            {
                Recorder.Records.Add("In Method");
            }

            public void ThrowsException()
            {
                Recorder.Records.Add("In Method");
                throw new Exception();
            }
        }

        internal sealed class SpyInterfaceAttributes : ISpy
        {
            [InterfaceIntercept(typeof(RecordingHandler))]
            public void InterceptedMethod()
            {
                Recorder.Records.Add("In Method");
            }

            public void ThrowsException()
            {
                throw new Exception();
            }
        }
    }
}